package com.perfma.xtest.lambda;

import com.google.common.collect.Lists;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author: junhui.si
 * @date: 2020-08-05 10:10
 **/
public class LambdaTest {
    public static void main(String[] args) {
        List<User> list = Lists.newArrayList();
        list.add(new User(1L, "1"));
        list.add(new User(2L, "2"));
        list.add(new User(3L, "3"));
        List<Long> idList = list.stream().map(User::getId).collect(Collectors.toList());
        System.out.println(idList);
        /**
         * (id1, id2) -> id1  若有重复id，则保留第一个。若没有这个，则重复id会报错
         */
        Map<Long, String> userMap = list.stream().collect(Collectors.toMap(User::getId, User::getName, (id1, id2) -> id1));
        Map<Long, User> userMap2 = list.stream().collect(Collectors.toMap(User::getId, user -> user, (id1, id2) -> id1));
        System.out.println(userMap);
        System.out.println(userMap2);

        list.stream().min(Comparator.comparingLong(User::getId)).ifPresent(user -> {
            System.out.println(user.getId());
            System.out.println(user.getName());
        });

        Optional<User> optionalMax = list.stream().max(Comparator.comparingLong(User::getId));
        optionalMax.ifPresent(System.out::println);

        Long sum = list.stream().mapToLong(User::getId).sum();
        System.out.println(sum);
        long a = 100_000_000;
        System.out.println(a);
    }
}
